﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
using System.Globalization;

namespace Migration.Classes
{
	public static class MigrationTaxonomies
	{
		private static TaxonomyManager m_TaxonomyManager;
		private static FlatTaxonomy m_TagsTaxonomy;
		private static HierarchicalTaxonomy m_CategoriesTaxonomy;

		static MigrationTaxonomies()
		{
			m_TaxonomyManager = TaxonomyManager.GetManager();
			m_TagsTaxonomy = m_TaxonomyManager.GetTaxonomies<FlatTaxonomy>().Single();
			m_CategoriesTaxonomy = m_TaxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Categories").Single();
		}

		private static FlatTaxon AddTagToTaxonomy(string tag, CultureInfo culture)
		{
			FlatTaxon checkTaxon = m_TaxonomyManager.GetTaxa<FlatTaxon>().Where(t => t.Name == tag).FirstOrDefault();
			if (checkTaxon == null)
			{
				checkTaxon = m_TaxonomyManager.CreateTaxon<FlatTaxon>();

				if (MigrationCultures.IsMultilingual)
                    checkTaxon.Title[culture] = tag;
				else
					checkTaxon.Title = tag;

				checkTaxon.Name = tag;
				checkTaxon.UrlName = MigrationUtils.GetUrlName(tag);

				m_TagsTaxonomy.Taxa.Add(checkTaxon);
				m_TaxonomyManager.SaveChanges();
			}

			return checkTaxon;
		}

		public static void AddTagsToOrganizer(List<string> tags, OrganizerBase organizer, CultureInfo culture)
		{
			if (m_TaxonomyManager.Provider == null)
			{
				m_TaxonomyManager = TaxonomyManager.GetManager();
			}

			for (int i = 0; i < tags.Count; i++)
			{
				if (!String.IsNullOrEmpty(tags[i]))
				{
					FlatTaxon taxon = AddTagToTaxonomy(tags[i], culture);
					if (!organizer.TaxonExists(m_TagsTaxonomy.Name, taxon.Id))
						organizer.AddTaxa(m_TagsTaxonomy.Name, new Guid[] { taxon.Id });
				}
			}
		}

		private static HierarchicalTaxon AddCategoryToTaxonomy(string category, CultureInfo culture)
		{
			HierarchicalTaxon checkTaxon = m_TaxonomyManager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == category).FirstOrDefault();
			if (checkTaxon == null)
			{
				checkTaxon = m_TaxonomyManager.CreateTaxon<HierarchicalTaxon>();

				if (MigrationCultures.IsMultilingual)
					checkTaxon.Title[culture] = category;
				else
					checkTaxon.Title = category;

				checkTaxon.Name = category;
				checkTaxon.UrlName = MigrationUtils.GetUrlName(category);
				
				m_CategoriesTaxonomy.Taxa.Add(checkTaxon);
				m_TaxonomyManager.SaveChanges();
			}

			return checkTaxon;
		}

		public static void AddCategoryToOrganizer(string category, OrganizerBase organizer, CultureInfo culture)
		{
			if (String.IsNullOrEmpty(category))
				return;

			if (m_TaxonomyManager.Provider == null)
			{
				m_TaxonomyManager = TaxonomyManager.GetManager();
			}

            HierarchicalTaxon taxon = AddCategoryToTaxonomy(category, culture);

			// Categories must be added as singular form ATM
			if (!organizer.TaxonExists("Category", taxon.Id))
				organizer.AddTaxa("Category", taxon.Id);
		}
	}
}
